package xin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import xin.constants.UserConstants;
import xin.entity.TOrgEmployee;
import xin.dao.TOrgEmployeeMapper;
import xin.handler.MyException;
import xin.service.TOrgEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author young
 * @since 2023年10月09日
 */
@Service
public class TOrgEmployeeServiceImpl extends ServiceImpl<TOrgEmployeeMapper, TOrgEmployee> implements TOrgEmployeeService {


    @Override
    public List<TOrgEmployee> findAll() {

        return this.baseMapper.selectList(null);
    }

    @Override
    public Integer add(TOrgEmployee tOrgEmployee) {

        return this.save(tOrgEmployee)?1:0;
    }

    @Override
    public Integer update(TOrgEmployee employee) {
        return this.baseMapper.updateById(employee);
    }

    @Override
    public Integer delete(Long id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public List<TOrgEmployee> findByNameLike(String keyWorld) {

        List<TOrgEmployee> user_name = this.baseMapper.selectList(new QueryWrapper<TOrgEmployee>().like("username", keyWorld));
        return user_name;
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public Map<String, TOrgEmployee> login(String username, String password) {

        TOrgEmployee tOrgEmployee = this.baseMapper.selectOne(new QueryWrapper<TOrgEmployee>().eq("username", username)
                .eq("password", password));

        if (tOrgEmployee==null) throw new MyException("账号名或者密码错误");


        String token = UUID.randomUUID().toString();

        tOrgEmployee.setEmail(null);
        tOrgEmployee.setPassword(null);
        tOrgEmployee.setId(null);

        UserConstants.map.put(token,tOrgEmployee);

        Map<String,TOrgEmployee> map = new HashMap<>();
        map.put(token,tOrgEmployee);

        return map;
    }
}
